package api;

import conf.APP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.HttpClientUtils;
import utils.ResponseData;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class TempApi {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(CommonApi.class);
    //获取图片验证码
    private static final String imageVerifyUrl = APP.BASE_URL_TEMP + "/common/public/verifycode1/";
    //获取短信验证码
    private static final String phoneVerifyUrl = APP.BASE_URL_TEMP + "/member/public/sendSms";
    //注册
    private static final String reginUrl = APP.BASE_URL_TEMP + "/member/public/reg";
    //登录
    private static final String loginUrl = APP.BASE_URL_TEMP + "/member/public/login";
    //是否登录
    private static final String isLoginUrl = APP.BASE_URL_TEMP + "/member/public/islogin";
    //实名认证
    private static final String realNameUrl = APP.BASE_URL_TEMP + "/member/realname/approverealname";
    //认证
    private static final String trustRegUrl = APP.BASE_URL_TEMP + "/trust/trust/register";


    /**获取图片验证码
     * @param random  随机数
     */
    public ResponseData getImageVerify(String random) throws Exception {
        String newUrl = imageVerifyUrl+random;
        logger.info("newUrl={}",newUrl);
        ResponseData responseData = HttpClientUtils.get(newUrl);
        logger.info("responseData={}",responseData);
        return responseData;

    }

    /**
     *获取短信验证码
     * @param phone 电话
     * @param imgVerifyCode 图片验证码
     * @param type 类型
     */
    public ResponseData getPhoneVerify(String phone,String imgVerifyCode,String type) throws Exception {
        logger.info("phone={} imgVerifyCode={} type={}",phone,imgVerifyCode,type);
        Map<String,String> form = new HashMap<>();
        form.put("phone",phone);
        form.put("imgVerifyCode",imgVerifyCode);
        form.put("type",type);
        ResponseData responseData = HttpClientUtils.post(phoneVerifyUrl, form);
        logger.info("responseData={}",responseData);
        return responseData;


    }

    /**注册
     *
     * @param phone 电话
     * @param password 密码
     * @param verifyCode 图片验证码8888
     * @param phone_code 短信验证码 666666
     * @param dyServer 是否同意协议[on/off]
     * @param invitePhone 邀请人
     */
    public ResponseData regin(String phone,String password
    ,String verifyCode,String phone_code,String dyServer,String invitePhone) throws Exception {

        Map<String,String> form = new HashMap<>();
        form.put("phone",phone);
        form.put("password",password);
        form.put("verifycode",verifyCode);
        form.put("phone_code",phone_code);
        form.put("dy_server",dyServer);
        form.put("invite_phone",invitePhone);
        ResponseData responseData = HttpClientUtils.post(reginUrl, form);
        logger.info("responseData={}",responseData);
        return responseData;

    }

    /**
     * 登录
     * @param keywords 手机号
     * @param password  密码
     */
    public ResponseData login(String keywords,String password) throws Exception {
        logger.info("keywords={} password={}",keywords,password);
        Map<String,String> form = new HashMap<>();
        form.put("keywords",keywords);
        form.put("password",password);
        ResponseData responseData = HttpClientUtils.post(loginUrl, form);
        logger.info("responseData={}",responseData);
        return responseData;
    }

    /**
     * 是否登录
     */
    public ResponseData isLogin() throws Exception {
        ResponseData responseData = HttpClientUtils.post(isLoginUrl, null);
        logger.info("responseData={}",responseData);
        return responseData;

    }
    //实名认证
    public ResponseData realName(String realname,String card_id) throws Exception {
        Map<String,String> params = new HashMap<>();
        params.put("realname",realname);
        params.put("card_id",card_id);
        return HttpClientUtils.postFile(realNameUrl,params,null);
    }

    public ResponseData trustReg() throws Exception {
        ResponseData responseData = HttpClientUtils.post(trustRegUrl, null);
        logger.info("responseData={}",responseData);
        return responseData;
    }
}
